package cis350.pcr;

import cis350.pcr.types.Type.SearchType;
import android.content.Context;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AutoCompleteAdapter extends CursorAdapter implements Filterable {
	private CodeLookupDB codeDB;
	private final int numDepts = 2;
	private final int numCourses = 3;
	private final int numProfs = 3;

	private int idIndex;
	private int titleIndex;
	private int subtitleIndex;
	private int typeIndex;

	private Drawable deptIcon;
	private Drawable courseIcon;
	private Drawable profIcon;

	public AutoCompleteAdapter(Context context, Cursor c) {
		super(context, c);
		codeDB = new CodeLookupDB(context);

		idIndex = c.getColumnIndex("_id");
		titleIndex = c.getColumnIndex("title");
		subtitleIndex = c.getColumnIndex("subtitle");
		typeIndex = c.getColumnIndex("type");

		deptIcon = context.getResources().getDrawable(R.drawable.dept);
		courseIcon = context.getResources().getDrawable(R.drawable.course);
		profIcon = context.getResources().getDrawable(R.drawable.person);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		String title = cursor.getString(titleIndex);
		String subtitle = cursor.getString(subtitleIndex);
		SearchType type = SearchType.values()[cursor.getInt(typeIndex)];

		ImageView icon = ((ImageView) ((LinearLayout) view).getChildAt(0));
		int color;

		if (type == SearchType.DEPT) {
			icon.setImageDrawable(deptIcon);
			color = 0xc0ff8800;
		} else if (type == SearchType.COURSE) {
			icon.setImageDrawable(courseIcon);
			color = 0xc0669900;
		} else {
			icon.setImageDrawable(profIcon);
			color = 0xc09933cc;
		}

		icon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
		// icon.setColorFilter(new LightingColorFilter(0, color));
		LinearLayout horizontal = (LinearLayout) ((LinearLayout) view)
				.getChildAt(1);

		((TextView) horizontal.getChildAt(0)).setText(title);
		((TextView) horizontal.getChildAt(1)).setText(subtitle);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		final LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final LinearLayout view = (LinearLayout) inflater.inflate(
				R.layout.autocomplete, parent, false);

		return view;
	}

	@Override
	public CharSequence convertToString(Cursor cursor) {
		int index = cursor.getColumnIndex("title");
		return cursor.getString(index);
	}

	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
		if (getFilterQueryProvider() != null) {
			return getFilterQueryProvider().runQuery(constraint);
		}

		String args = "";

		if (constraint != null) {
			args = constraint.toString();
		}

		return codeDB.getSearchCursor(args, numDepts, numCourses, numProfs);
	}

	public String getTerm(int position) {
		Cursor cursor = (Cursor) getItem(position);
		return cursor.getString(titleIndex);
	}

	public String getCode(int position) {
		Cursor cursor = (Cursor) getItem(position);
		String code = cursor.getString(idIndex);
		SearchType type = SearchType.values()[cursor.getInt(typeIndex)];

		if (type == SearchType.DEPT)
			return "depts/" + code;
		else if (type == SearchType.COURSE)
			return "coursehistories/" + code + "/reviews";
		else
			return "instructors/" + code + "/reviews";
	}

	public SearchType getType(int position) {
		Cursor cursor = (Cursor) getItem(position);
		SearchType type = SearchType.values()[cursor.getInt(typeIndex)];
		return type;
	}

	public void close() {
	}
}
